﻿CREATE PROCEDURE [acms].[Post_Delete]
	@Id int,
	@VersionId timestamp
AS
	Update [Posts]
	Set Deleted = 1
	Where Id = @Id
		and VersionId = @VersionID
		and Deleted = 0
		
	if @@RowCount <> 1 begin
		if Exists (Select null From Post_View Where Id = @id)
			return -100
		else
			return -200
	end
		
	declare @ParentId int
	
	Select @ParentId = ParentId FROM [Posts] where Id = @Id
	
	update [Posts]
		set Sequence = b.RowNumber
	from [Posts] a inner join
		(Select Id, Row_Number() Over (Order By Sequence) - 1 as RowNumber
		From [Posts]
	Where ParentId = @ParentId
		and Deleted = 0
		) b on a.Id = b.Id
RETURN 0;